package org.dreamwork.assistant.tools.persistence.gui;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;

/**
 * Created by seth.yang on 2017/6/22
 */
public class DBUtil {
    public static Connection getSQLite (File db) {
        try {
            Class.forName ("org.sqlite.JDBC");
            return DriverManager.getConnection ("jdbc:sqlite:" + db.getCanonicalPath ());
        } catch (Exception ex) {
            throw new RuntimeException (ex);
        }
    }

    public static Connection getDerby (File dir) {
        try {
            Class.forName ("org.apache.derby.jdbc.EmbeddedDriver");
            return DriverManager.getConnection ("jdbc:derby:" + dir.getCanonicalPath () + ";create=true");
        } catch (Exception e) {
            throw new RuntimeException (e);
        }
    }

    public static Connection getPostgreSQL (String url, String userName, String password) {
        try {
            Class.forName ("org.postgresql.Driver");
            return DriverManager.getConnection (url, userName, password);
        } catch (Exception ex) {
            throw new RuntimeException (ex);
        }
    }
}